package com.yzy.soft.config;

import com.alibaba.fastjson.JSONObject;
import com.yzy.soft.system.domain.Percent;
import com.yzy.soft.system.monitor.Memory;
import com.yzy.soft.system.monitor.ParticularMemoryInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@EnableScheduling
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
    private static final Logger logger=LoggerFactory.getLogger(WebSocketConfig.class);
    private Percent percent=new Percent();

    private Percent percent2=new Percent();
    private Percent percent3=new Percent();

    private List<Percent> percentList=new ArrayList<>();

    String[] strings=new String[]{"主数据提取","明细数据提取","变更状态","主数据补充","明细数据补充"};
    private Memory memory=new Memory();

    private int i=0;
    private int success=0;
    private int error=0;
    private int all=0;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
//        registry.addEndpoint("/endpointWisely").withSockJS();
        registry.addEndpoint("/vue").setAllowedOrigins("*").withSockJS();
//        registry.addEndpoint("/test").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic","/task","/dsa","/user");
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user");
    }
    //@Scheduled(fixedRate = 1000)
    //@SendTo("/topic/callback")
    public Object callback() throws Exception {
        // 发现消息
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //messagingTemplate.convertAndSend("/topic/callback", df.format(new Date())+strings[i%4]);
        i++;
        return "callback";
    }

    @Scheduled(fixedRate = 1000)
    //@SendTo("/topic/callback")
    public Object callback1() throws Exception {
        // 发现消息
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String info= JSONObject.toJSONString((ParticularMemoryInfo)memory.getMemoryInfo());
        messagingTemplate.convertAndSend("/topic/monitor/memory",info );
        return "callback";
    }
    @Scheduled(fixedRate = 1000)
//    @SendTo("/topic/callback")
    public Object callback2() throws Exception {
        // 发现消息
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String info= JSONObject.toJSONString((ParticularMemoryInfo)memory.getMemoryInfo());
        if(i>=101){
            i=-1;
        }
        if(success>=101){
            success=-1;
        }
        if(error>=101){
            error=-1;
        }
        if(all>=101){
            all=-1;
        }
        String taskId="333333";
        percent.setTaskId(taskId);
        percent.setAll(all);
        percent.setError(error);
        percent.setSuccess(success);
        percent.setI(i);


        String taskId2="444444";
        percent2.setTaskId(taskId2);
        percent2.setAll(all);
        percent2.setError(error);
        percent2.setSuccess(success);
        percent2.setI(i);

        String taskId3="555555";
        percent3.setTaskId(taskId3);
        percent3.setAll(all);
        percent3.setError(error);
        percent3.setSuccess(success);
        percent3.setI(i);

        percentList.add(percent);
        percentList.add(percent2);

        percentList.add(percent3);
//        String info= JSONObject.toJSONString(percent);
        String info= JSONObject.toJSONString(percentList);
        String info2= JSONObject.toJSONString(percent2);
        logger.info("当前地 任务值为：{}", i++);
         success+=1;
    error+=1;
       all+=1;
//        messagingTemplate.convertAndSend("/task/userqq",i++ );
        messagingTemplate.convertAndSendToUser(taskId,"/dsa/wq",info );
        messagingTemplate.convertAndSendToUser(taskId2,"/dsa/wqwwww",info2 );
        percentList.clear();
        return "callback2";
    }






}
